package com.teach.controller.shop;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.teach.R;
import com.teach.dto.PhotoFormDTO;
import com.teach.dto.ProductSearch;
import com.teach.dto.ShopOrderItemSearch;
import com.teach.entity.Product;
import com.teach.entity.ProductClass;
import com.teach.entity.ShopOrder;
import com.teach.entity.ShopOrderItem;
import com.teach.entity.ShopSlide;
import com.teach.service.FileService;
import com.teach.service.ShopProductService;
import com.teach.service.ShopSlideService;
import com.teach.service.shop.ShopAdminServlet;
import com.teach.service.shop.ShopIndexService;
import com.teach.service.shop.ShopOrederService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/shop")
@Slf4j
public class ShopAdminController {
	
	List<ProductClass> productClasses = new ArrayList<ProductClass>();
	
	List<ShopSlide> shopSlide = new ArrayList<ShopSlide>();
	
	@Autowired
	ShopIndexService sis;
	
	@Autowired
	ShopAdminServlet shopAdmin;
	
	@Autowired
	ShopProductService shopProduct;
	
	@Autowired
	ShopSlideService shopSS;
	
	@Autowired
	ShopOrederService shopOrder;
	
	@RequestMapping("/product-class-list.s")
	@ResponseBody
	public List<ProductClass> doList(){
		return sis.findAll();
	}
	@Autowired
	FileService fileService;
	@RequestMapping("/slide-class-list.s")
	@ResponseBody
	public List<ShopSlide> doListSlide(){
		return sis.slideFindAll();
	}
	
	@RequestMapping("/order-page-ajax.s")
	@ResponseBody
	public Object doListOrder(ShopOrderItemSearch search){
		log.debug("{}", search);
		return shopOrder.page(search);
	}
	
	@RequestMapping("/product-page-ajax.s")
	@ResponseBody
	public Object doAjaxPage(ProductSearch search){
		//NewsSearch search = RequestUtil.loadParams(request, NewsSearch.class);
		log.debug("{}", search);
		//System.out.println("456");
		//log.debug("",shopAdmin.page(search));
		return shopAdmin.page(search);
	}
	
	@PostMapping("/modify.do")
	@ResponseBody
	public R doModify(Product product) {
		shopAdmin.modify(product);
		return R.ok();
	}
	
	@PostMapping("/delete.do")
	@ResponseBody
	public R doDelete(Product product) {
		shopAdmin.doDelete(product);
		return R.ok();
	}
	
	@PostMapping("/product-class-modify.s")
	@ResponseBody
	public R doProductClass(Product product){
		shopAdmin.doProductClass(product);
		return R.ok();
	}
	
	@PostMapping("/modify-product-list.do")
	@ResponseBody
	public Product doModifyProduct(Long productId){
		return shopAdmin.doModifyProduct(productId);
	}
	
	@PostMapping("/slide-photo.do")
	@ResponseBody
	public  R doSlidePhoto(ShopSlide shopSlide,MultipartFile img){
		shopSS.doSlidePhoto(shopSlide,img);
		return R.ok();
	}

	@PostMapping("/product-update.do")
	@ResponseBody
	public R doUpdateUpload(Product form,MultipartFile img){
		shopProduct.updateProduct(form, img);
		return R.ok();
	}
	@PostMapping("/add-product.do")
	@ResponseBody
	public R doAddUpload(Product form,MultipartFile img){
		shopProduct.addProduct(form, img);
		return R.ok();
	}
	
	@PostMapping("/order-fh.do")
	@ResponseBody
	public R doOrderFH(Long shopOrderItemId,int shopOrderItemState){
		ShopOrderItem item = new ShopOrderItem();
		item.setShopOrderItemId(shopOrderItemId);
		item.setShopOrderItemState(shopOrderItemState);
		shopOrder.doOrderFH(item);
		return R.ok();
	}
	
	@PostMapping("/order-xg.do")
	@ResponseBody
	public R doOrderXG(Long shopOrderItemId,String shopOrderItemAddress){
		ShopOrderItem item = new ShopOrderItem();
		item.setShopOrderItemId(shopOrderItemId);
		item.setShopOrderItemAddress(shopOrderItemAddress);
		shopOrder.doOrderFH(item);
		return R.ok();
	}
	
}
